{extend name="public/layout" /}
{block name="head"}{/block}
{block name="body"}

<body>
  <div class="container-fluid p-t-15">
    <div class="row">
      <div class="col-sm-4 col-md-3 col-lg-2">
        <div class="card" id="left-menu" style="height:calc(100vh - 40px);overflow-y: auto;">
          <div class="card-header p-10">
            <div class="btn-group btn-group-sm">
              <button type="button" class="btn btn-default" onclick="doAction.add()"><i
                  class="mdi mdi-plus"></i></button>
              <button type="button" class="btn btn-default" onclick="doAction.edit();"><i
                  class="mdi mdi-table-edit"></i></button>
              <button type="button" class="btn btn-default" onclick="doAction.del();"><i
                  class="mdi mdi-delete"></i></button>
            </div>
          </div>
          <ul class="ztree" id="ztree" style="padding-bottom:20px;"></ul>
        </div>
      </div>
      <div class="col-sm-8 col-md-9 col-lg-10">
        <div class="card">
          <div class="card-body p-10">
            <form id="search-form" class="form-inline">
              <input type="hidden" id="class_id" value="" />
              <input type="hidden" id="class_code" name="class_code@=" value="-1" />
            </form>
            <div class="toolbar">
              <a href="javascript:void(0);" class="btn btn-default" onclick="doAction.addDict();">
                <i class="mdi mdi-plus"></i> 添加
              </a>
              <a href="javascript:void(0);" class="btn btn-default" onclick="doAction.delDict();">
                <i class="mdi mdi-archive"></i> 删除
              </a>
            </div>
            <div class="table-responsive">
              <table id="grid" class="table">
                <thead>
                  <tr>
                    <th data-field="checkbox" data-checkbox="true"></th>
                    <th data-field="dict_code">字典编码</th>
                    <th data-field="dict_value">字典值</th>
                    <th data-field="show_order" data-width="80">排序</th>
                    <th data-field="operate" data-width="180" data-formatter="doAction.formatterOperate">操作
                    </th>
                  </tr>
                </thead>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script>
    helper.bootstrapTable({
      pagination: false,
      sortName: "show_order",
      sortOrder: "asc",
      height: null,
      url: '/system/dict/index.html'
    });
    var doAction = {
      initTree: function () {
        Do('ztree', function () {
          $.get('/system/dict_class/index.html', function (nodes) {
            $.fn.zTree.init($('#ztree'), {
              data: {
                view: { showLine: false },
                simpleData: { enable: true, idKey: "id", pIdKey: "pid", rootPId: 0 }
              },
              callback: {
                onClick: function (event, treeId, treeNode) {
                  $('#class_id').val(treeNode.id);
                  $('#class_code').val(treeNode.code);
                  helper.refreshTable();
                }
              }
            }, nodes);
            var zTree = $.fn.zTree.getZTreeObj("ztree");
            zTree.expandAll(true);
          });
        });
      },
      add: function () {
        layer.open({
          type: 2,
          offset: "30px",
          area: helper.layerMedia('800px','370px'),
          content: "/system/dict_class/add.html"
        });
      },
      edit: function () {
        var classId = $('#class_id').val();
        if (classId == '') {
          helper.tips('请选择字典分类');
          return false;
        }
        layer.open({
          type: 2,
          offset: "30px",
          area: helper.layerMedia('800px','370px'),
          content: "/system/dict_class/edit.html?id=" + classId
        });
      },
      del: function () {
        var classId = $('#class_id').val();
        if (classId == '') {
          helper.tips('请选择字典分类');
          return false;
        }
        layer.confirm("确认删除该分类吗?", { icon: 3 }, function (index) {
          helper.getAjax({
            url: "/system/dict_class/del.html?id=" + classId,
            success: function (res) {
              if (res.code == 1) {
                doAction.initTree();
              } else {
                helper.tips(res.msg);
              }
            }
          });
          layer.close(index);
        });
      },
      addDict: function () {
        var classId = $('#class_id').val();
        if (classId == '') {
          helper.tips('请选择字典分类');
          return false;
        }
        layer.open({
          type: 2,
          offset: "30px",
          area: helper.layerMedia('800px','320px'),
          content: "/system/dict/add.html?class_id=" + classId
        });
      },
      editDict: function (id) {
        layer.open({
          type: 2,
          offset: "30px",
          area: helper.layerMedia('800px','320px'),
          content: "/system/dict/edit.html?id=" + id
        });
      },
      delDict: function (id) {
        var ids = id;
        if (valid.isEmpty(id)) {
          ids = $.map($('#grid').bootstrapTable('getSelections'), function (row, index) {
            return row.id;
          });
          if (ids.length == 0) {
            layer.msg('请选择操作字典');
            return false;
          }
          ids = ids.join(',');
        }
        layer.confirm("确认删除该字典吗?", { icon: 3 }, function (index) {
          helper.getAjax({
            url: "/system/dict/del.html",
            data: { id: ids },
            success: function (res) {
              if (res.code == 1) {
                helper.refreshTable();
              } else {
                helper.tips(res.msg);
              }
            }
          });
          layer.close(index);
        });
      },
      formatterOperate: function (value, row, index) {
        var html = '';
        html += '<a href="javascript:void(0);"  class="btn-xs btn btn-primary" onclick="doAction.editDict(' + row.id + ')">' +
          '<i class="mdi mdi-pencil"></i> 编辑</a> ';
        html += '<a href="javascript:void(0);"  class="btn-xs btn btn-danger" onclick="doAction.delDict(' + row.id + ')">' +
          '<i class="mdi mdi-archive"></i> 删除</a> ';
        return html;
      }
    };
    doAction.initTree();
  </script>
</body>
{/block}